package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.RoleService;
import com.sl.au.service.UserService;

@RestController
@RequestMapping(value = "/ui/account") 
public class AccountController extends BaseController {
 
	@Autowired
	AccountService accountService;
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;
	@Autowired
	AccountRepository accountRepository;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("account/accountindex");
		modelAndView.addObject("users", userService.getRepository().getNoAccount());
		modelAndView.addObject("roles", roleService.getRepository().findAll());

		return modelAndView;
	}
	
	///验证账号是否重复        
		@RequestMapping(value="acountvalidate",method = RequestMethod.GET)
		public Map<String, Object> codeValidate(HttpServletRequest request){
			Map<String, Object> map=new HashMap<String, Object>();
			String accountName=request.getParameter("accountName");
			long count=accountService.getRepository().getAccountCount(accountName);
			if(count>0){
				map.put("code", 0);	
			}else{
				map.put("code", 1);
			}
			return map;
		}
	

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<Account> accounts = accountService.getPage(start, length, key, "createTime", Sort.Direction.DESC);
		int count = accountService.getCount(key);
		map.put("data", accounts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> saveAccount(HttpServletRequest request, @RequestBody Account account) {
		Map<String, Object> map = new HashMap<String, Object>();
		int r=0;
		User user=account.getUser();
		user=userService.getRepository().getOne(user.getId());
		if(null!=user){
			String name=user.getEmpLogin().getUserName();
			String password=user.getEmpLogin().getPassword();
			account.setAccountName(name);
			account.setPassword(password);
			if(!StringUtils.hasText(name) || !StringUtils.hasText(password)){
				map.put("code", -1);
				map.put("message", "保存失败，请确认用户账号信息的完整性！");
				return map;
			}
			accountService.getRepository().save(account);
			
			Role role=roleService.getRepository().findOne(account.getRole().getId());
			String position=role.getName();
			user.setPosition(position);
			userService.getRepository().save(user);
			
			map.put("code",r);
		} 	
		return map;
	}

}
